﻿#ifndef SQLTHREAD_H
#define SQLTHREAD_H

#include <QThread>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDateTime>
#include "../../include/injectordata.h"
#include "../../include/dtudata.h"
#include "../../include/serialpack.h"
#include "../ui/chart.h"

class SqlThread : public QThread
{
    Q_OBJECT
public:
    SqlThread(const QString&, const QString&);
    ~SqlThread();

protected:
    virtual void run() override;

public slots:
    void slt_addInjector(const InjectorData& data);
    void slt_deleteInjector(uint id);
    void slt_updateInjectorSetting(const InjectorSetting& setting, uint id);
    void slt_updateInjectorInfo(const QString& name, const QString& pos, uint id);
    void slt_updateInjectorStatus(const SerialPack& pack, uint id);

    void slt_addDTU(const DTUData& data);
    void slt_deleteDTU(uint id);
    void slt_updateDTU(const DTUData& data);

    void slt_uploadCurrent(uint id, const QList<int>& data);
    void slt_searchCurrent(const QDateTime& start, const QDateTime& end, uint id);

signals:
    void sig_lastId(int);
    void sig_searchCurrent(const QList<QVector<QPointF>>&);

private:
    QString database;
    QString connection;
    uint get_lastId();
    void execute(const QString& str);

    QList<QVector<QPointF>> currents;
};

#endif // SQLTHREAD_H
